//
//  FiltrateView.m
//  HuaXin
//
//  Created by 江伟 on 2018/11/2.
//

#import "FiltrateView.h"
@interface FiltrateView()
@property (nonatomic,strong) LeftTextImgBtnView *allBtnView;
@property (nonatomic,strong) LeftTextImgBtnView *btnView2;
@property (nonatomic,strong) LeftTextImgBtnView *btnView3;
@end

@implementation FiltrateView

- (void)setTitles:(NSArray *)titles {
    _titles = titles;
    _allBtnView.title = _titles[0];
    _btnView2.title = _titles[1];
    _btnView3.title = _titles[2];
}

- (void)setOrignalTitleColor:(UIColor *)orignalTitleColor {
    _orignalTitleColor = orignalTitleColor;
    _allBtnView.lab.textColor = _orignalTitleColor;
    _btnView3.lab.textColor = _orignalTitleColor;
    _btnView2.lab.textColor = _orignalTitleColor;
}

- (void)setOriginalImages:(NSArray *)originalImages {
    _originalImages = originalImages;
    _allBtnView.img.image = IMG(originalImages[0]);
    _btnView2.img.image = IMG(originalImages[1]);
    _btnView3.img.image = IMG(originalImages[2]);
}

- (void)setDefalutSelIndex:(NSInteger)defalutSelIndex {
    if (defalutSelIndex == 0) {
        [self tapAllBtnViewGesture:_allBtnView.tap];
    } else if (defalutSelIndex == 1) {
        [self tapAllBtnViewGesture:_btnView2.tap];
    } else {
        [self tapAllBtnViewGesture:_btnView3.tap];
    }
}

- (void)setupViews {
    _allBtnView = [[LeftTextImgBtnView alloc] initWithFrame:CGRectMake(0, 0, KWidth/3.0, self.height) space:7 imgSize:CGSizeMake(aufont(7), aufont(5))];
    [self addSubview:_allBtnView];
    _allBtnView.index = 0;
    [_allBtnView addTapGestureTarget:self action:@selector(tapAllBtnViewGesture:)];
    
    _btnView2 = [[LeftTextImgBtnView alloc] initWithFrame:CGRectMake(KWidth/3.0, 0, KWidth/3.0, self.height) space:7 imgSize:CGSizeMake(aufont(7), aufont(14))];
    [self addSubview:_btnView2];
    _btnView2.index = 1;
    [_btnView2 addTapGestureTarget:self action:@selector(tapBtnView2Gesture:)];
    
    _btnView3 = [[LeftTextImgBtnView alloc] initWithFrame:CGRectMake(KWidth/3.0 * 2.0, 0, KWidth/3.0, self.height) space:7 imgSize:CGSizeMake(aufont(7), aufont(14))];
    [self addSubview:_btnView3];
    _btnView3.index = 1;
    [_btnView3 addTapGestureTarget:self action:@selector(tapBtnView3Gesture:)];
}

- (void)tapAllBtnViewGesture:(UITapGestureRecognizer *)tap {
    [self setOriginalImages:_originalImages];
    [self setOrignalTitleColor:_orignalTitleColor];
    if (self.selTitleColor) {
        _allBtnView.lab.textColor = self.selTitleColor;
    }
    
    _btnView2.state = 0;
    _btnView3.state = 0;
    if (_allBtnView.state == 0 || _allBtnView.state == 2) {
        _allBtnView.state = 1;
        if (self.selImagesState1.count > 0) {
            _allBtnView.img.image = IMG(self.selImagesState1[0]);
        }
    } else if (_allBtnView.state == 1) {
        _allBtnView.state = 2;
        if (self.selImagesState2.count > 0) {
            _allBtnView.img.image = IMG(self.selImagesState2[0]);
        }
    }
    
    [self.delegate filtrateView:self didSelectBtnViewIndex:0 state:_allBtnView.state];
}

- (void)tapBtnView2Gesture:(UITapGestureRecognizer *)tap {
    [self setOriginalImages:_originalImages];
    [self setOrignalTitleColor:_orignalTitleColor];
    if (self.selTitleColor) {
        _btnView2.lab.textColor = self.selTitleColor;
    }
    
    _allBtnView.state = 0;
    _btnView3.state = 0;
    if (_btnView2.state == 0 || _btnView2.state == 2) {
        _btnView2.state = 1;
        if (self.selImagesState1.count > 0) {
            _btnView2.img.image = IMG(self.selImagesState1[1]);
        }
    } else if (_btnView2.state == 1) {
        _btnView2.state = 2;
        if (self.selImagesState2.count > 0) {
        _btnView2.img.image = IMG(self.selImagesState2[2]);
        }
    }
    
    [self.delegate filtrateView:self didSelectBtnViewIndex:1 state:_btnView2.state];
}

- (void)tapBtnView3Gesture:(UITapGestureRecognizer *)tap {
    [self setOriginalImages:_originalImages];
    [self setOrignalTitleColor:_orignalTitleColor];
    if (self.selTitleColor) {
        _btnView3.lab.textColor = self.selTitleColor;
    }
    
    _allBtnView.state = 0;
    _btnView2.state = 0;
    if (_btnView3.state == 0 || _btnView3.state == 2) {
        _btnView3.state = 1;
        if (self.selImagesState1.count > 0) {
            _btnView3.img.image = IMG(self.selImagesState1[2]);
        }
    } else if (_btnView3.state == 1) {
        _btnView3.state = 2;
        if (self.selImagesState2.count > 0) {
            _btnView3.img.image = IMG(self.selImagesState2[2]);
        }
    }
    
     [self.delegate filtrateView:self didSelectBtnViewIndex:2 state:_btnView3.state];
}

- (LeftTextImgBtnView *)btnViewForIndex:(NSInteger)index {
    if (index == 0) {
        return _allBtnView;
    } else if (index == 1) {
        return _btnView2;
    } else {
        return _btnView3;
    }
}

@end
